// src/book/book.controller.ts
import { Controller, Get, Param, ParseIntPipe, Req } from '@nestjs/common';
import { BookService } from './book.service';
// import { Pagination } from 'nestjs-typeorm-paginate';
// import { Book } from './book.entity';
import type { Request } from 'express';
import { PaginateOptions } from '../../common/decorators/pagination.decorator'; // 导入装饰器
import type { PaginationOptions } from '../../types/book';
import { resFormatMethod } from '../../utils/res-format';

import type { BookListResponse } from '../../types/book';
@Controller('book')
export class BookController {
  constructor(private readonly bookService: BookService) {}

  @Get('list')
  async getList(
    @PaginateOptions() options: PaginationOptions, // 使用装饰器注入
    @Req() req: Request
  ): Promise<BookListResponse> {
    // 构建基础路由，保留除 page/limit 外的所有参数（search/sort/order 等）
    const url = new URL(
      `${req.protocol}://${req.get('host')}${req.originalUrl}`
    );
    // 实际上page和limit没有消失 存放在options中 这只是将路由变干净 方便next的分页库控制page和limit参数
    url.searchParams.delete('page');
    url.searchParams.delete('limit');
    const baseRoute = url.toString();

    const paginationData = await this.bookService.paginate(options, baseRoute);

    // 使用你的格式化函数包装结果
    return resFormatMethod(0, '获取图书列表成功', paginationData);
  }

  @Get(':id')
  getBook(@Param('id', ParseIntPipe) id: number) {
    return this.bookService.findOne(id);
  }
}
